package com.qcu.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CustomDateDeserializer extends JsonDeserializer<Date> {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    @Override
    public Date deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        JsonNode node = parser.getCodec().readTree(parser);
        if (node.isTextual()) {
            try {
                return dateFormat.parse(node.asText());
            } catch (ParseException e) {
                throw new IOException("Invalid date format", e);
            }
        } else if (node.isObject()) {
            String dateStr = node.get("value").asText(); // 假设对象中有"value"字段
            try {
                return dateFormat.parse(dateStr);
            } catch (ParseException e) {
                throw new IOException("Invalid date format in object", e);
            }
        }
        throw new IOException("Unsupported JSON token for Date");
    }
}
